package q226_invertTree;

import CommonClass.Common.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution_3 {
    // 广度搜索解决问题
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return root;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();

            // 交换每层的左右子树
            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;

            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        }
        return root;


    }
}
